Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Telephony

Namespace C4F.DevKit.Telephony.Controls
	Public Partial Class DTMFPanel
		Inherits UserControl
		#Region "Members"

		Private _tapiManager As TapiManager

		#End Region

		#Region "Properties"

		Public Property TapiManager() As TapiManager
			Get
				Return _tapiManager
			End Get

			Set(ByVal value As TapiManager)
				_tapiManager = value
			End Set
		End Property

		<Browsable(True)> _
		Public Property DtmfEnabled() As Boolean
			Get
				Return groupBoxDigitDetection.Enabled
			End Get

			Set(ByVal value As Boolean)
				groupBoxDigitDetection.Enabled = value
				groupBoxDigitGeneration.Enabled = value
			End Set
		End Property

		#End Region

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
			AddHandler chkStartDigitDetection.CheckedChanged, AddressOf chkStartDigitDetection_CheckedChanged
			AddHandler btnClear.Click, AddressOf btnClear_Click
			AddHandler btnGenerate.Click, AddressOf btnGenerate_Click
			AddHandler txtDigitDetection.TextChanged, AddressOf txtDigitDetection_TextChanged
		End Sub

		#End Region

		#Region "Methods"

		Public Sub ChangeStateOfClearButton()
			If txtDigitDetection.Text <> String.Empty Then
				btnClear.Enabled = True
			Else
				btnClear.Enabled = False
			End If
		End Sub

		Public Sub DigitDetectionTextBoxUpdate(ByVal digitObject As Object)
			Dim digit As Char = CChar(digitObject)
			txtDigitDetection.Text += digit.ToString()
		End Sub

		#End Region

		#Region "Event Handlers"

		Private Sub chkStartDigitDetection_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
			Dim chkDigit As CheckBox = CType(sender, CheckBox)
			If chkDigit.Checked = True Then
				txtDigitDetection.Enabled = True

				ChangeStateOfClearButton()
				Try
					_tapiManager.StartDigitDetection()
				Catch exception As Exception
					MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
				End Try
			Else
				txtDigitDetection.Enabled = False
				btnClear.Enabled = False
				Try
					_tapiManager.StopDigitDetection()
				Catch exception As Exception
					MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
				End Try
			End If
		End Sub

		Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs)
			txtDigitDetection.Text = String.Empty
		End Sub

		Private Sub btnGenerate_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				_tapiManager.GenerateDigits(txtDigitGeneration.Text)
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub txtDigitDetection_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
			ChangeStateOfClearButton()
		End Sub

		#End Region
	End Class
End Namespace
